import 'dart:convert';

import 'package:awesome_project/utils/strings.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:intl/intl.dart' as intl;

import '../models/export_models.dart';

class Utils {
  static void showToast(String content) {
    SmartDialog.showToast(content,
        alignment: Alignment.center, displayType: SmartToastType.last);
  }


  static void showErrorToast(String content, List<int> errCodes, int errCode) {
    if (!errCodes.contains(errCode)) {
      showToast(content);
    }
  }

  static String combineBaseUrls(String dioBaseUrl, String? baseUrl,
      {bool isFull = false}) {
    if (isFull) return "";
    if (baseUrl == null || baseUrl.trim().isEmpty) {
      return dioBaseUrl;
    }

    final url = Uri.parse(baseUrl);

    if (url.isAbsolute) {
      return url.toString();
    }

    return Uri.parse(dioBaseUrl).resolveUri(url).toString();
  }




  static int getSeconds(String? dt, {String? newPattern}) {
    if (isBlank(dt)) return 0;
    return (newPattern == null
            ? DateTime.parse(dt!)
            : intl.DateFormat(newPattern).parse(dt!))
        .difference(DateTime.now())
        .inSeconds;
  }

  static String constructTime(int millis) {
    const number = 1000;
    int millisUntilFinished = millis;
    int day = 0;
    //获取天数
    if (millisUntilFinished > 60 * 60 * 24 * number) {
      day = millisUntilFinished ~/ (60 * 60 * 24 * number);
      millisUntilFinished -= day * (60 * 60 * 24 * number);
    }
    int hour = millisUntilFinished ~/ (60 * 60 * number);
    int minute =
        (millisUntilFinished - hour * 60 * 60 * number) ~/ (60 * number);
    int second = (millisUntilFinished -
            hour * 60 * 60 * number -
            minute * 60 * number) ~/
        number;
    if (second >= 60) {
      second %= 60;
      minute += second ~/ 60;
    }
    if (minute >= 60) {
      minute %= 60;
      hour += (minute ~/ 60);
    }
    String sd = formatTime(day);
    String sh = formatTime(hour);
    String sm = formatTime(minute);
    String ss = formatTime(second);
    //
    //天大于0
    if (day > 0) {
      return '$sd天 $sh:$sm:$ss';
    } else {
      //天小于0
      //小时也为0
      if ('00' == sh) {
        if ('00' == sm) {
          return '00:00:$ss';
        } else {
          return '00:$sm:$ss';
        }
      } else {
        //小时不为0
        return '$sh:$sm:$ss';
      }
    }
  }

//数字格式化，将 0~9 的时间转换为 00~09
  static String formatTime(int timeNum) {
    return timeNum < 10 ? '0$timeNum' : timeNum.toString();
  }

  static Future<List<CityModel>> getCityModelS() async {
    final cityS = await rootBundle.loadString("assets/pca.json");
    final cityJson = json.decode(cityS) as List<dynamic>;
    return cityJson.map((e) => CityModel.fromJson(e)).toList();
  }

  static ({Offset offset, Size size}) getOffsetAndSize(
      BuildContext targetContext) {
    final renderBox = targetContext.findRenderObject() as RenderBox;
    return (offset: renderBox.localToGlobal(Offset.zero), size: renderBox.size);
  }
  static String? getCookie(String? rawCookie){
    String? sessionId;
    if (rawCookie != null) {
      var cookies = rawCookie.split(';');
      for (var cookie in cookies) {
        cookie = cookie.trim();
        if (cookie.startsWith('session_id=')) {
          sessionId = cookie.substring('session_id='.length);
          break;
        }
      }
    }
    return sessionId;
  }
}
